In the world of machine learning and computer vision, the process of making sense out of visual data is called 'inference' or 'prediction'. Ultralytics YOLO11 offers a powerful feature known as predict mode that is tailored for high-performance, real-time inference on a wide range of data sources.
观看: How to Extract the Outputs from Ultralytics YOLO Model for Custom Projects.
真实世界的应用制造业体育安全车辆备件检测足球运动员检测人员坠落检测为什么使用Ultralytics YOLO 进行推理?Here's why you should consider YOLO11's predict mode for your various inference needs:
多功能性:能够对图像、视频甚至实时流进行推断。Performance: Engineered for real-time, high-speed processing without sacrificing accuracy.易用性:直观的Python 和CLI 界面,便于快速部署和测试。高度可定制:各种设置和参数可根据您的具体要求调整模型的推理行为。预测模式的主要功能YOLO11's predict mode is designed to be robust and versatile, featuring:
兼容多种数据源:无论您的数据是单个图像、图像集合、视频文件还是实时视频流,预测模式都能满足您的需求。流媒体模式: 使用流功能生成具有内存效率的 Results 对象。通过设置 stream=True 在预测器的调用方法中。批处理:批量处理:能够一次性处理多个图像或视频帧,进一步加快推理时间。易于集成:凭借灵活的应用程序接口,可轻松与现有数据管道和其他软件组件集成。Ultralytics YOLO 模型返回Python Results 对象,或一个内存效率高的Python Results 当 stream=True 会在推理过程中传递给模型:
预测
from ultralytics import YOLO# Load a modelmodel = YOLO("yolo11n.pt") # pretrained YOLO11n model# Run batched inference on a list of imagesresults = model(["image1.jpg", "image2.jpg"]) # return a list of Results objects# Process results listfor result in results:boxes = result.boxes # Boxes object for bounding box outputsmasks = result.masks # Masks object for segmentation masks outputskeypoints = result.keypoints # Keypoints object for pose outputsprobs = result.probs # Probs object for classification outputsobb = result.obb # Oriented boxes object for OBB outputsresult.show() # display to screenresult.save(filename="result.jpg") # save to diskfrom ultralytics import YOLO# Load a modelmodel = YOLO("yolo11n.pt") # pretrained YOLO11n model# Run batched inference on a list of imagesresults = model(["image1.jpg", "image2.jpg"], stream=True) # return a generator of Results objects# Process results generatorfor result in results:boxes = result.boxes # Boxes object for bounding box outputsmasks = result.masks # Masks object for segmentation masks outputskeypoints = result.keypoints # Keypoints object for pose outputsprobs = result.probs # Probs object for classification outputsobb = result.obb # Oriented boxes object for OBB outputsresult.show() # display to screenresult.save(filename="result.jpg") # save to disk推理源YOLO11 can process different types of input sources for inference, as shown in the table below. The sources include static images, video streams, and various data formats. The table also indicates whether each source can be used in streaming mode with the argument stream=True ✅.流模式有利于处理视频或实时流,因为它会创建一个结果生成器,而不是将所有帧加载到内存中。
提示
使用 stream=True 用于处理长视频或大型数据集,以有效管理内存。当 stream=False在这种情况下,所有帧或数据点的结果都会存储在内存中,这可能会迅速累加,并导致大量输入出现内存不足错误。与此形成鲜明对比的是 stream=True 利用生成器,它只将当前帧或数据点的结果保存在内存中,从而大大减少了内存消耗并防止出现内存不足的问题。
资料来源示例类型说明图像'image.jpg'str 或 Path单个图像文件。网址'https://ultralytics.com/images/bus.jpg'str图片的 URL。截图'screen'str截图PILImage.open('image.jpg')PIL.Image具有 RGB 通道的 HWC 格式。OpenCVcv2.imread('image.jpg')np.ndarray带有 BGR 频道的 HWC 格式 uint8 (0-255).numpynp.zeros((640,1280,3))np.ndarray带有 BGR 频道的 HWC 格式 uint8 (0-255).torchtorch.zeros(16,3,320,640)torch.Tensor带 RGB 通道的 BCHW 格式 float32 (0.0-1.0).CSV'sources.csv'str 或 Path包含图像、视频或目录路径的 CSV 文件。视频 ✅'video.mp4'str 或 PathMP4 和 AVI 等格式的视频文件目录 ✅'path/'str 或 Path包含图像或视频的目录路径。球体 ✅'path/*.jpg'str全局模式来匹配多个文件。使用 * 字符作为通配符。YouTube ✅'https://youtu.be/LNwODJXcvt4'strYouTube 视频的 URL。流 ✅'rtsp://example.com/media.mp4'str流媒体协议(如 RTSP、RTMP、TCP)的 URL 或 IP 地址。多流 ✅'list.streams'str 或 Path*.streams 文